/**
 *
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，vip文档中已提供路由的基础图标与小清新图标的配置方案，请仔细阅读
 */

import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layouts'
import EmptyLayout from '@/layouts/EmptyLayout'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },
  // {
  //   path: '/register',
  //   component: () => import('@/views/register/index'),
  //   hidden: true,
  // },
  {
    path: '/401',
    name: '401',
    component: () => import('@/views/401'),
    hidden: true,
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    hidden: true,
  },
  {
    path: '/',
    component: Layout,
    redirect: 'index',
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/index/index'),
        meta: {
          title: '首页',
          icon: 'home',
          affix: true,
        },
      },
      {
        path: 'personalCenter',
        name: 'PersonalCenter',
        hidden: true,
        component: () => import('@/views/personalCenter/index'),
        meta: {
          title: '个人中心',
        },
      },
    ],
  },
]

export const asyncRoutes = [
  {
    path: '/system',
    component: Layout,
    redirect: 'noRedirect',
    name: 'system',
    alwaysShow: true,
    meta: {
      title: '系统管理',
      icon: 'users-cog',
      permissions: ['systemManagement'],
    },
    children: [
      {
        path: 'dept',
        name: 'Dept',
        component: () => import('@/views/system/dept/index'),
        meta: {
          title: '组织管理',
          permissions: ['system:sysDept:query'],
        },
      },
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/system/user/index'),
        meta: {
          title: '用户管理',
          permissions: ['system:sysUser:query'],
        },
      },
      {
        path: 'post',
        name: 'Post',
        component: () => import('@/views/system/post/index'),
        meta: {
          title: '岗位管理',
          permissions: ['system:sysPost:query'],
        },
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/system/role/index'),
        meta: {
          title: '角色管理',
          permissions: ['system:sysRole:query'],
        },
      },
      {
        path: 'menu',
        name: 'Menu',
        component: () => import('@/views/system/menu/index'),
        meta: {
          title: '菜单管理',
          permissions: ['menuManagement'],
        },
      },
    ],
  },
  {
    path: '/task',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    name: 'task',
    meta: {
      title: '我的事务',
      icon: 'file-signature',
      permissions: ['taskManagement'],
    },
    children: [
      {
        path: 'todo',
        name: 'TaskTodo',
        component: () => import('@/views/task/todo/index'),
        meta: {
          title: '待办任务',
          permissions: ['flow:todoTask:query'],
        },
      },
      {
        path: 'done',
        name: 'TaskDone',
        component: () => import('@/views/task/done/index'),
        meta: {
          title: '已办任务',
          permissions: ['flow:doneTask:query'],
        },
      },
      // {
      //   path: 'copy',
      //   name: 'TaskCopy',
      //   component: () => import('@/views/task/copy/index'),
      //   meta: {
      //     title: '抄送事项',
      //     permissions: ['sendTaskManagement'],
      //   },
      // },
    ],
  },
  {
    path: '/workflow',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    name: 'workflow',
    meta: {
      title: '流程管理',
      icon: 'vector-square',
      permissions: ['flowManagement'],
    },
    children: [
      {
        path: 'modeler',
        name: 'Modeler',
        component: () => import('@/views/workflow/modeler/index'),
        meta: {
          title: '流程设计',
          permissions: ['flow:wfModel:query'],
        },
      },
      {
        path: 'category',
        name: 'ModelCategory',
        component: () => import('@/views/workflow/category/index'),
        meta: {
          title: '分类管理',
          permissions: ['flow:wfCategory:query'],
        },
      },
    ],
  },
  {
    path: '/basics',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    name: 'Basics',
    meta: {
      title: '基础档案',
      icon: 'coins',
      permissions: ['basicsManagement'],
    },
    children: [
      {
        path: 'category',
        name: 'Category',
        component: () => import('@/views/basics/category/index'),
        meta: {
          title: '类别管理',
          permissions: ['basics:basCategory:query'],
        },
      },
      {
        path: 'unit',
        name: 'Unit',
        component: () => import('@/views/basics/unit/index'),
        meta: {
          title: '计量单位',
          permissions: ['basics:basUnit:query'],
        },
      },
      {
        path: 'product',
        name: 'Product',
        component: () => import('@/views/basics/product/index'),
        meta: {
          title: '产品管理',
          permissions: ['basics:basProduct:query'],
        },
      },
      {
        path: 'clientele',
        name: 'Clientele',
        component: () => import('@/views/basics/clientele/index'),
        meta: {
          title: '客户管理',
          permissions: ['basics:salClientele:query'],
        },
      },
    ],
  },
  {
    path: '/sales',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    name: 'Sales',
    meta: {
      title: '销售管理',
      icon: 'border-all',
      permissions: ['salesManagement'],
    },
    children: [
      {
        path: 'order',
        name: 'Order',
        component: () => import('@/views/sales/order/index'),
        meta: {
          title: '销售订单',
          permissions: ['sales:salOrder:query'],
        },
      },
    ],
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true,
  },
]

const router = new VueRouter({
  base: publicPath,
  mode: routerMode,
  scrollBehavior: () => ({
    y: 0,
  }),
  routes: constantRoutes,
})
//注释的地方是允许路由重复点击，如果你觉得框架路由跳转规范太过严格可选择放开
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

export function resetRouter() {
  router.matcher = new VueRouter({
    base: publicPath,
    mode: routerMode,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: constantRoutes,
  }).matcher
}

export default router
